﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using SF.DBUtility;
using SF.Common;
namespace SF.DAL
{
    /// <summary>
    /// 数据访问类:sf_reply_sends
    /// </summary>
    public partial class sf_reply_sends
    {
        public sf_reply_sends()
        { }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from sf_reply_sends");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(SF.Model.sf_reply_sends model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into sf_reply_sends(");
            strSql.Append("busnieseNum,appNum,title,msgId,mediaId,msgType,userType,msgNum,uriMsg,textMsg,userMsg,addTime)");
            strSql.Append(" values (");
            strSql.Append("@busnieseNum,@appNum,@title,@msgId,@mediaId,@msgType,@userType,@msgNum,@uriMsg,@textMsg,@userMsg,@addTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					new SqlParameter("@title", SqlDbType.NVarChar,50),
					new SqlParameter("@msgId", SqlDbType.NChar,10),
					new SqlParameter("@mediaId", SqlDbType.NVarChar,200),
					new SqlParameter("@msgType", SqlDbType.Int,4),
					new SqlParameter("@userType", SqlDbType.Int,4),
					new SqlParameter("@msgNum", SqlDbType.Int,4),
					new SqlParameter("@uriMsg", SqlDbType.NVarChar,-1),
					new SqlParameter("@textMsg", SqlDbType.NVarChar,-1),
					new SqlParameter("@userMsg", SqlDbType.Text),
					new SqlParameter("@addTime", SqlDbType.DateTime)};
            parameters[0].Value = model.busnieseNum;
            parameters[1].Value = model.appNum;
            parameters[2].Value = model.title;
            parameters[3].Value = model.msgId;
            parameters[4].Value = model.mediaId;
            parameters[5].Value = model.msgType;
            parameters[6].Value = model.userType;
            parameters[7].Value = model.msgNum;
            parameters[8].Value = model.uriMsg;
            parameters[9].Value = model.textMsg;
            parameters[10].Value = model.userMsg;
            parameters[11].Value = model.addTime;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SF.Model.sf_reply_sends model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update sf_reply_sends set ");
            strSql.Append("busnieseNum=@busnieseNum,");
            strSql.Append("appNum=@appNum,");
            strSql.Append("title=@title,");
            strSql.Append("msgId=@msgId,");
            strSql.Append("mediaId=@mediaId,");
            strSql.Append("msgType=@msgType,");
            strSql.Append("userType=@userType,");
            strSql.Append("msgNum=@msgNum,");
            strSql.Append("uriMsg=@uriMsg,");
            strSql.Append("textMsg=@textMsg,");
            strSql.Append("userMsg=@userMsg");
            strSql.Append("addTime=@addTime");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					new SqlParameter("@title", SqlDbType.NVarChar,50),
					new SqlParameter("@msgId", SqlDbType.NChar,10),
					new SqlParameter("@mediaId", SqlDbType.NVarChar,200),
					new SqlParameter("@msgType", SqlDbType.Int,4),
					new SqlParameter("@userType", SqlDbType.Int,4),
					new SqlParameter("@msgNum", SqlDbType.Int,4),
					new SqlParameter("@uriMsg", SqlDbType.NVarChar,-1),
					new SqlParameter("@textMsg", SqlDbType.NVarChar,-1),
					new SqlParameter("@userMsg", SqlDbType.Text),
					new SqlParameter("@addTime", SqlDbType.DateTime),
					new SqlParameter("@id", SqlDbType.BigInt,8)};
            parameters[0].Value = model.busnieseNum;
            parameters[1].Value = model.appNum;
            parameters[2].Value = model.title;
            parameters[3].Value = model.msgId;
            parameters[4].Value = model.mediaId;
            parameters[5].Value = model.msgType;
            parameters[6].Value = model.userType;
            parameters[7].Value = model.msgNum;
            parameters[8].Value = model.uriMsg;
            parameters[9].Value = model.textMsg;
            parameters[10].Value = model.userMsg;
            parameters[11].Value = model.addTime;
            parameters[12].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from sf_reply_sends ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from sf_reply_sends ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.sf_reply_sends GetModel(long id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,busnieseNum,appNum,title,msgId,mediaId,msgType,userType,msgNum,uriMsg,textMsg,userMsg,addTime from sf_reply_sends ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;

            SF.Model.sf_reply_sends model = new SF.Model.sf_reply_sends();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.sf_reply_sends DataRowToModel(DataRow row)
        {
            SF.Model.sf_reply_sends model = new SF.Model.sf_reply_sends();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = long.Parse(row["id"].ToString());
                }
                if (row["busnieseNum"] != null)
                {
                    model.busnieseNum = row["busnieseNum"].ToString();
                }
                if (row["appNum"] != null)
                {
                    model.appNum = row["appNum"].ToString();
                }
                if (row["title"] != null)
                {
                    model.title = row["title"].ToString();
                }
                if (row["msgId"] != null)
                {
                    model.msgId = row["msgId"].ToString();
                }
                if (row["mediaId"] != null)
                {
                    model.mediaId = row["mediaId"].ToString();
                }
                if (row["msgType"] != null && row["msgType"].ToString() != "")
                {
                    model.msgType = int.Parse(row["msgType"].ToString());
                }
                if (row["userType"] != null && row["userType"].ToString() != "")
                {
                    model.userType = int.Parse(row["userType"].ToString());
                }
                if (row["msgNum"] != null && row["msgNum"].ToString() != "")
                {
                    model.msgNum = int.Parse(row["msgNum"].ToString());
                }
                if (row["uriMsg"] != null)
                {
                    model.uriMsg = row["uriMsg"].ToString();
                }
                if (row["textMsg"] != null)
                {
                    model.textMsg = row["textMsg"].ToString();
                }
                if (row["userMsg"] != null)
                {
                    model.userMsg = row["userMsg"].ToString();
                }
                if (row["addTime"] != null && row["addTime"].ToString() != "")
                {
                    model.addTime = DateTime.Parse(row["addTime"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,busnieseNum,appNum,title,msgId,mediaId,msgType,userType,msgNum,uriMsg,textMsg,userMsg,addTime ");
            strSql.Append(" FROM sf_reply_sends ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,busnieseNum,appNum,title,msgId,mediaId,msgType,userType,msgNum,uriMsg,textMsg,userMsg,addTime ");
            strSql.Append(" FROM sf_reply_sends ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM sf_reply_sends ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from sf_reply_sends T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  BasicMethod
        #region  ExtensionMethod

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM sf_reply_sends");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
        #endregion  ExtensionMethod
    }
}

